﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem;

namespace BGSpaceShooter.View
{
    public class BillboardDrawer : IDrawer
    {
        private StarFighter starFighter;
        private StarFighter playerFighter;
        private readonly string playerId;
        private static readonly Texture2D texture = GameContentManager.Content.GetTexture("target");
        private static readonly SpriteFont font = GameContentManager.Content.GetFont();

        public BillboardDrawer(StarFighter starFighter, StarFighter playerFighter, string playerId)
        {
            this.starFighter = starFighter;
            this.playerFighter = playerFighter;
            this.playerId = playerId;
        }

        public void Draw(Camera camera)
        {
            SpriteBatch spriteBatch;
            BasicEffect basicEffect;

            spriteBatch = new SpriteBatch(Display.GraphicsDevice);
            basicEffect = new BasicEffect(Display.GraphicsDevice)
            {
                TextureEnabled = true,
                VertexColorEnabled = true,
            };
            
            Matrix modelMatrix = Matrix.Identity * starFighter.AdditionalTransformations * playerFighter.RotationMatrix * Matrix.CreateScale(starFighter.Scale * 0.3f) * Matrix.CreateTranslation(starFighter.Position);

            basicEffect.World = Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(-texture.Width / 2, texture.Height / 2, 0) * Matrix.CreateRotationX((float)Math.PI / 2) * modelMatrix;
            basicEffect.View = camera.CameraMatrix;
            basicEffect.Projection = Display.ProjectionMatrix;
            
            spriteBatch.Begin(0, null, null, DepthStencilState.DepthRead, RasterizerState.CullNone, basicEffect);
            spriteBatch.Draw(texture, Vector2.Zero, Color.GreenYellow);
            spriteBatch.End();

            basicEffect.World = Matrix.CreateScale(-1, -1, 1) * Matrix.CreateTranslation(texture.Width / 2, -texture.Height / 2, 0) * Matrix.CreateRotationX((float)Math.PI / 2) * modelMatrix;
            spriteBatch.Begin(0, null, null, DepthStencilState.DepthRead, RasterizerState.CullNone, basicEffect);
            spriteBatch.DrawString(font, playerId, Vector2.Zero, Color.GreenYellow);
            spriteBatch.End();
        }

        public IEnumerable<EffectDrawer> GetEffectDrawers()
        {
            return new List<EffectDrawer>();
        }
    }
}
